package com.example.myapplication.navigation.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.NavDirections;
import androidx.navigation.NavOptions;
import androidx.navigation.fragment.NavHostFragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.myapplication.MainGraphDirections;
import com.example.myapplication.R;
import com.example.myapplication.databinding.FragmentThirdBinding;
import com.example.myapplication.utils.NameCreator;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class ThirdFragment extends BaseLifeFragment {

    FragmentThirdBinding binding;

    public ThirdFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentThirdBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        binding.thirdFragmentBtnBack.setOnClickListener(v -> {
            log("###################################click() thirdFragmentBtnBack()###################################");
            NavHostFragment.findNavController(this).popBackStack();
        });

        binding.thirdFragmentBtnGoFirst.setOnClickListener(v -> {
            log("###################################click() thirdFragmentBtnGoFirst()###################################");
            popToGlobalFirstFragment();
        });

        binding.thirdFragmentBtnGoNewFirst.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                log("###################################click() thirdFragmentBtnGoNewFirst()###################################");
                popToGlobalNewFirstFragment();
            }
        });

        binding.thirdFragmentBtnGoFourFragment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                log("###################################click() thirdFragmentBtnGoFourFragment()###################################");
                NavHostFragment.findNavController(ThirdFragment.this).navigate(R.id.action_ThirdFragment_to_fourFragment);
            }
        });

        binding.thirdFragmentBtnGoFourFragmentAndPopUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                log("###################################click() thirdFragmentBtnGoFourFragmentAndPopUp()###################################");
                NavHostFragment.findNavController(ThirdFragment.this).navigate(R.id.action_ThirdFragment_to_fourFragment_and_popUp);
            }
        });
    }

    /**
     * 导航到开始位置，之前的开始试图会被弹出，重建一个开始试图
     */
    private void popToNewStart() {
        NavController controller = NavHostFragment.findNavController(this);
        int start = controller.getGraph().getStartDestination();
        NavOptions options = new NavOptions.Builder().setPopUpTo(start, true).build();
        controller.navigate(start, null, options);
    }

    /**
     * 导航到开始位置，之前的开始视图不会出栈，而会重新出现在用户面前。
     */
    private void popToStart() {
        NavController controller = NavHostFragment.findNavController(this);
        int start = controller.getGraph().getStartDestination();
        controller.popBackStack(start, true);
    }

    /**
     * 导航到开始位置，之前的开始视图不会出栈，而会重新出现在用户面前。
     */
    private void popToGlobalFirstFragment() {

        NavHostFragment.findNavController(this).navigate(R.id.action_global_popUpTo_FirstFragment);
    }

    /**
     * 导航到开始位置，之前的开始试图会被弹出，重建一个开始试图
     */
    private void popToGlobalNewFirstFragment() {
//        NavHostFragment.findNavController(this).navigate(R.id.action_global_popUpTo_new_FirstFragment);
        NavDirections directions= MainGraphDirections.actionGlobalPopUpToNewFirstFragment().setResult("从third 页面返回了，创建了新的firstFragment");
        NavHostFragment.findNavController(this).navigate(directions);
    }


    @Override
    public String initLogHead() {
        return NameCreator.getInstance().getName("***********ThirdFragment", hashCode());
    }
}
